package cn.com.blueInfo.bpm.system.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.system.entity.DynamicForm;
import cn.com.blueInfo.bpm.system.enums.DynamicFormType;
import cn.com.blueInfo.bpm.system.mapper.DynamicFormMapper;
import cn.com.blueInfo.bpm.system.repository.DynamicFormRepository;
import cn.com.blueInfo.bpm.system.request.DynamicFormDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 动态表单表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:17:41
 */
@Log4j2
@Service
public class DynamicFormRepositoryImpl
        extends ServiceImpl<DynamicFormMapper, DynamicForm>
        implements DynamicFormRepository, BaseRepository<DynamicForm, DynamicFormDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增动态表单
     * @param dynamicForm 动态表单参数
     */
    @Override
    public Integer createDynamicForm(DynamicForm dynamicForm) {
        return sqlExecuteResult(save(dynamicForm));
    }

    /**
     * 批量新增动态表单
     * @param dynamicFormList 动态表单参数列表
     */
    @Override
    public Integer batchCreateDynamicForm(List<DynamicForm> dynamicFormList) {
        return sqlExecuteResult(saveBatch(dynamicFormList), dynamicFormList.size());
    }

    /**
     * 删除动态表单
     * @param dynamicFormId 动态表单ID
     */
    @Override
    public Integer deleteDynamicForm(Long dynamicFormId) {
        DynamicForm dynamicForm = new DynamicForm();
        dynamicForm.setId(dynamicFormId);
        dynamicForm.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(dynamicForm));
    }

    /**
     * 批量删除动态表单
     * @param dynamicFormIdList 动态表单ID列表
     */
    @Override
    public Integer batchDeleteDynamicForm(List<Long> dynamicFormIdList) {
        List<DynamicForm> dynamicFormList = new ArrayList<>();
        for (Long dynamicFormId : dynamicFormIdList) {
            DynamicForm dynamicForm = new DynamicForm();
            dynamicForm.setId(dynamicFormId);
            dynamicForm.setDeleted(BaseDeleted.DELETED);
            dynamicFormList.add(dynamicForm);
        }
        return sqlExecuteResult(removeBatchByIds(dynamicFormList), dynamicFormList.size());
    }

    /**
     * 更新部分动态表单
     * @param dynamicForm 动态表单参数
     */
    @Override
    public Integer updateDynamicFormPartial(DynamicForm dynamicForm) {
        validateUpdateId(dynamicForm.getId());
        LambdaUpdateWrapper<DynamicForm> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(DynamicForm::getId, dynamicForm.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
     * 更新全部动态表单
     * @param dynamicForm 动态表单参数
     */
    @Override
    public Integer updateDynamicForm(DynamicForm dynamicForm) {
        validateUpdateId(dynamicForm.getId());
        return sqlExecuteResult(updateById(dynamicForm));
    }

    /**
     * 查询一条动态表单
     * @param dynamicFormId 动态表单ID
     */
    @Override
    public DynamicForm getDynamicFormById(Long dynamicFormId) {
        LambdaQueryWrapper<DynamicForm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DynamicForm::getId, dynamicFormId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询动态表单
     * @param dynamicFormQueryDTO 动态表单参数
     */
    @Override
    public Page<DynamicForm> getDynamicFormPage(DynamicFormDTO.QueryDTO dynamicFormQueryDTO) {
        Page<DynamicForm> dynamicFormPage = new Page<>(
                dynamicFormQueryDTO.getPage(), dynamicFormQueryDTO.getSize());
        LambdaQueryWrapper<DynamicForm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dynamicFormQueryDTO);

        BaseDeleted deleted = dynamicFormQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(dynamicFormPage, queryWrapper);
        } else {
            return page(dynamicFormPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 动态表单 数量
     * @param dynamicFormQueryDTO 动态表单参数
     */
    @Override
    public Long getDynamicFormCount(DynamicFormDTO.QueryDTO dynamicFormQueryDTO) {
        LambdaQueryWrapper<DynamicForm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dynamicFormQueryDTO);

        BaseDeleted deleted = dynamicFormQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 动态表单
     * @param dynamicFormQueryDTO 动态表单参数
     */
    @Override
    public List<DynamicForm> getDynamicFormList(DynamicFormDTO.QueryDTO dynamicFormQueryDTO) {
        LambdaQueryWrapper<DynamicForm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dynamicFormQueryDTO);

        BaseDeleted deleted = dynamicFormQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<DynamicForm> queryWrapper,
                              DynamicFormDTO.QueryDTO dynamicFormQueryDTO) {

        String formCode = dynamicFormQueryDTO.getFormCode();
        queryWrapper.eq(StringUtils.isNotBlank(formCode), DynamicForm::getFormCode, formCode);

        String formName = dynamicFormQueryDTO.getFormName();
        queryWrapper.eq(StringUtils.isNotBlank(formName), DynamicForm::getFormName, formName);

        DynamicFormType formType = dynamicFormQueryDTO.getFormType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(formType), DynamicForm::getFormType, formType);

        String description = dynamicFormQueryDTO.getDescription();
        queryWrapper.eq(StringUtils.isNotBlank(description), DynamicForm::getDescription, description);

        addQueryWrapper(queryWrapper, dynamicFormQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<DynamicForm, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                DynamicForm::getOrderNumber,
                DynamicForm::getTenantId,
                DynamicForm::getApplicationId,
                DynamicForm::getStatus,
                DynamicForm::getDeleted,
                DynamicForm::getDeleteUserId,
                DynamicForm::getDeleteTime,
                DynamicForm::getRemark,
                DynamicForm::getCreateUserId,
                DynamicForm::getCreateTime,
                DynamicForm::getUpdateUserId,
                DynamicForm::getUpdateTime,
                DynamicForm::getExtend1,
                DynamicForm::getExtend2,
                DynamicForm::getExtend3);
    }

}
